//
//  QGCityListViewController.m
//  QuanGou
//
//  Created by qqqq on 15/11/22.
//  Copyright © 2015年 DongYongFei. All rights reserved.
//

#import "QGCityListViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface QGCityListViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *cityListTableView;

@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) NSMutableArray *hotCityArr;
@property (nonatomic, strong) NSMutableArray *sectionArr;
@end

@implementation QGCityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.listArr = [NSMutableArray array];
    self.hotCityArr = [NSMutableArray array];
    self.sectionArr = [NSMutableArray array];
    
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CityList.plist" ofType:nil]];
    self.hotCityArr = root[@"hotlist"];
    self.listArr = root[@"list"];
    for (NSDictionary *dic in self.listArr) {
        
        [self.sectionArr addObject:dic[@"areaName"]];
    }
    [self.sectionArr insertObject:@"热" atIndex:0];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(back:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];

}
- (void)back:(UISwipeGestureRecognizer *)swipe {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sectionArr[section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listArr.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.hotCityArr.count;
    }else {
        NSDictionary *dic = self.listArr[section - 1];
        NSArray *arr = dic[@"city"];
        return arr.count;
    }
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionArr;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue2) reuseIdentifier:cellID];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = self.hotCityArr[indexPath.row][@"cityName"];
    }else {
        NSArray *arr = self.listArr[indexPath.section - 1][@"city"];
        cell.textLabel.text = arr[indexPath.row][@"cityName"];
    }
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    
    NSString *city;
    if (indexPath.section == 0) {
        city = self.hotCityArr[indexPath.row][@"cityName"];
    }else {
        NSArray *arr = self.listArr[indexPath.section - 1][@"city"];
        city = arr[indexPath.row][@"cityName"];
    }
    
    [geocoder geocodeAddressString:[NSString stringWithFormat:@"中国%@", city] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLLocation *location = [[placemarks lastObject] location];
        if ([self.cityListDelegate respondsToSelector:@selector(cityName:latitude:longitude:)]) {
            [self.cityListDelegate cityName:city latitude:location.coordinate.latitude longitude:location.coordinate.longitude];
        }
        [self back:nil];
//        NSLog(@"%f,--%f",location.coordinate.latitude,location.coordinate.longitude);
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
